package com.example.testdbsearchform;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ProdactListActivity extends Activity {
	
	ProductDAO datasource;
	CustomSQLiteHelper dbHelper;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        
        dbHelper = CustomSQLiteHelper.getInstance(this);
        SQLiteDatabase database = dbHelper.open(); 
        datasource = new ProductDAO(database);
        
        Intent intent = getIntent();
		Bundle request = intent.getExtras();
		
		String name = null;
		String product_name = request.getString("product_name");
		if (product_name != null && !product_name.equals("")) {
			name = product_name;
		}
		Long category = null;
		Long product_category = request.getLong("product_category");
		if (product_category != null && !product_category.equals(0L)) {
			category = product_category;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy");
		String product_date = request.getString("product_date");
		Date date = null;
		try {
			if (product_date != null && !product_date.equals("")) {
				date = sdf.parse(product_date);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 Toast.makeText(getApplicationContext(), "name " + product_name +
				" category" + category + " date " +date, Toast.LENGTH_LONG) .show();
		 
		// Get the application instance
        List<Product> list = datasource.getAllProducts(name, category, date);
        ListView mainListView = (ListView) findViewById(R.id.productListView);
        mainListView.setAdapter(new CustomArrayAdapter(this, list));
                
	}
	
	
}
